package com.twu.biblioteca;

public class Book {

    int isbn;
    String bookName;
    String author;
    String yearPublished;

    Customer customer;
    boolean available;

    private static final boolean FAILURE = false;
    private static final boolean SUCCESS = true;


    public Book(int isbn, String bookName, String author, String yearPublished) {

        if(bookName.equals("") || author.equals("") || yearPublished.equals(""))
            throw new RuntimeException();

        this.isbn = isbn;
        this.bookName = bookName;
        this.author = author;
        this.yearPublished = yearPublished;

        this.customer = null;
        this.available = true;
    }

    public boolean isAvailable() {
        return this.available ? true : false;
    }

    public boolean checkoutIfAvailable(Customer customer) {

        if(this.available) {
            this.available = false;
            this.customer = customer;
            return SUCCESS;
        }
        return FAILURE;
    }

    public boolean returnIfValid() {
        if (!this.available) {
            this.available = true;
            this.customer = null;
            return SUCCESS;
        }
        return FAILURE;
    }

    public String getName() {
        return this.bookName;
    }

    public String toString() {
        StringBuilder outputString = new StringBuilder();

        outputString.append(this.bookName);
        outputString.append(" ");

        outputString.append(this.author);
        outputString.append(" ");

        outputString.append(this.yearPublished);

        return outputString.toString();
    }
}
